class Prom {
    value = undefined;
    state = 'PENDING';
    thenQ = [];

    static resolve(value) {
        if (value?.then) return value;
        return new Prom(resolve => resolve(value))
    }

    constructor(executor) {
        const resolve = value => { setTimeout(() => {
                this.state = 'RESOLVED'
                this.value = value
                this.thenQ.forEach(
                    ({fn, resolve}) => resolve(fn(value))
                )
        }) }
        executor(resolve)
    }

    then(fn) {
        if (this.state === 'PENDING') {
            return new Prom((resolve) => {
                this.thenQ.push({fn, resolve})
            })
        }else{
            return Prom.resolve(fn(this.value))
        }
    }
}